package com.crb.crm.manage.service.biz.impl;

import com.crb.crm.manage.utils.DateUtil;
import com.crb.crm.manage.dao.biz.CrmReqLogInfoMapper;
import com.crb.crm.manage.dao.biz.CrmReqLogMapper;
import com.crb.crm.manage.entity.biz.CrmReqLogDTO;
import com.crb.crm.manage.entity.biz.CrmReqLogSearchDTO;
import com.crb.crm.manage.pojo.biz.CrmReqLogWithBLOBs;
import com.crb.crm.manage.service.biz.CrmReqLogService;
import com.crb.crm.manage.utils.PageDataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Package: com.crb.crm.manage.service.biz.impl
 * Description：
 * Author: huangjian
 * Date:  2020/9/3 15:58
 * Modified By:
 * @author huangjian
 */
@Service
public class CrmReqLogServiceImpl implements CrmReqLogService {
    private static final Logger logger = LoggerFactory.getLogger(CrmReqLogServiceImpl.class);
    @Autowired
    private CrmReqLogMapper mapper;
    @Autowired
    private CrmReqLogInfoMapper infoMapper;

    @Override
    public PageDataResult getCrmReqLogs(CrmReqLogSearchDTO searchDTO, int page, int limit) {
        handleParam(searchDTO);
        PageDataResult pdr = new PageDataResult();
        PageHelper.startPage(page, limit);
        List<CrmReqLogDTO> urList = infoMapper.getCrmReqLogs(searchDTO);
        // 获取分页查询后的数据
        PageInfo<CrmReqLogDTO> pageInfo = new PageInfo<>(urList);
        // 设置获取到的总记录数total：
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pdr.setList(urList);
        return pdr;
    }


    private void handleParam(CrmReqLogSearchDTO searchDTO) {
        // 时间处理
        if (null != searchDTO) {
            if (StringUtils.isNotEmpty(searchDTO.getCreateDateStart())
                    && StringUtils.isEmpty(searchDTO.getCreateDateEnd())) {
                searchDTO.setCreateDateEnd(DateUtil.format(new Date()));
            } else if (StringUtils.isEmpty(searchDTO.getCreateDateStart())
                    && StringUtils.isNotEmpty(searchDTO.getCreateDateEnd())) {
                searchDTO.setCreateDateStart(DateUtil.format(new Date()));
            }
            if (StringUtils.isNotEmpty(searchDTO.getCreateDateStart())
                    && StringUtils.isNotEmpty(searchDTO.getCreateDateEnd())) {
                if (searchDTO.getCreateDateEnd().compareTo(searchDTO.getCreateDateStart()) < 0) {
                    String temp = searchDTO.getCreateDateStart();
                    searchDTO.setCreateDateStart(searchDTO.getCreateDateEnd());
                    searchDTO.setCreateDateEnd(temp);
                }
            }
        }
    }

    @Override
    public String delCrmReqLog(String id) {
        mapper.deleteByPrimaryKey(id);
        return "ok";
    }

    @Override
    public String getLog(String  id, String logType) {
        CrmReqLogWithBLOBs crmReqLogWithBLOBs = mapper.selectByPrimaryKey(id);
        if (!Objects.isNull(crmReqLogWithBLOBs)) {
            return "showCrmReqData".equals(logType) ? crmReqLogWithBLOBs.getData() : crmReqLogWithBLOBs.getContent();
        }
        return null;
    }
}
